import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const token = localStorage.getItem('token')
const store = new Vuex.Store({
  state: {
    token: '',
    userName: '',
    headers: {
      'Authorization': `Bearer ${token}`
    }
  },
  getters: {
    getToken (state) {
      return state.token || localStorage.getItem('token') || ''
    }
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
      console.info('index localstore保存token成功')
    },
    delToken (state) {
      state.token = ''
      localStorage.removeItem('token')
    },
    setUserInfo (state, userName) {
      state.userName = userName
    }
  }
})

export default store
